﻿
$(document).ready(function () {

    $(".info").find(".support").css("display", "none");
    $(".info").hover(function () {
        $(this).find(".support").css("display", "block");
    }, function () {
        $(this).find(".support").css("display", "none");
    });

    $(".s-tits").find("span").hover(function () {
        $(this).addClass("hover");
    }, function () {
        $(this).removeClass("hover");
    });
    $("img").error(function () {
        $(this).attr("src", "/Content/Images/no-result.png");
    });

    //点击回到顶部的元素
    $("#gotop").click(function (e) {
        //以1秒的间隔返回顶部
        $('body,html').animate({ scrollTop: 0 }, 1000);
    });
    $("#gotop").mouseover(function (e) {
        $(this).css("background", "url(../Content/Images/backtop.png) no-repeat 0px 0px");
    });
    $("#gotop").mouseout(function (e) {
        $(this).css("background", "url(../Content/Images/backtop.png) no-repeat -70px 0px");
    });
    goTop();//实现回到顶部元素的渐显与渐隐

});
//Tab控制函数
function tabs(tabObj) {
    var tabNum = $(tabObj).parent().index();

    //设置点击后的切换样式
    $(tabObj).parent().parent().find("li a").removeClass("selected");
    $(tabObj).addClass("selected");
    //根据参数决定显示内容
    $(".tab-content").hide();
    $(".tab-content").eq(tabNum).show();
}
//写Cookie
function addCookie(objName, objValue, objHours) {
    var str = objName + "=" + escape(objValue);
    if (objHours > 0) {//为0时不设定过期时间，浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = objHours * 3600 * 1000;
        date.setTime(date.getTime() + ms);
        str += "; expires=" + date.toGMTString();
    }
    document.cookie = str;
}

//读Cookie
function getCookie(objName) {//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == objName) return unescape(temp[1]);
    }
    return "";
}

//图片大小
function imgResize(obj, size) {
    $obj = $(obj);
    $obj.width() > $obj.height() ? ($obj.css("width", size + "px")) : ($obj.css("height", size + "px"));
}

function zanclick(prodcutId) {
    var loadUrl = "/tools/WebAjax.ashx?systemId=" + prodcutId + "&action=zanclick";
    var key = getCookie('Praise_System_Id_' + prodcutId);
    if (key == null || key == undefined || key != "true") {
        jQuery.ajax({
            url: loadUrl,
            scriptCharset: "utf-8",
            success: function (data) {
                var a = eval('(' + data + ')');
                if (a == null) {
                    return;
                }
                var up = a.praise;
                if (up > 10000) {
                    up = parseInt(up / 10000);
                    up = up + '万';
                } else if (up > 1000) {
                    up = parseInt(up / 1000);
                    up = up + '千';
                }
                addCookie('Praise_System_Id_' + prodcutId, 'true', 0);
                var t = (parseInt($("#support_" + prodcutId).position().left) + 10, parseInt($("#support_" + prodcutId).position().top) - 10, $("#support_" + prodcutId));
                return $("#support_" + prodcutId).append('<div class="add"><b>+1</b></div>'), $(".add").css({
                    position: "absolute",
                    "z-index": "100",
                    left: "20px",
                    top: "-20px"
                }),
                $(".add").animate({ left: 0, top: 10, opacity: 0 }, 100, function () {
                    t.find("span").text(up);
                    $("#shadePraiseLi_" + prodcutId).find("span").html(up);
                }),
                $("#support_" + prodcutId).addClass("cur");
            }
        });
    } else {
        $.dialog.tips("已点赞");
        $("#support_" + prodcutId).addClass("cur");
    };
}

function guanzhuclick(prodcutId, memberID) {
    //未登录，请先登录
    if (memberID == 0) {
        $.dialog.tips("请先登录!", 1, "tips.gif", function () {
            window.location.href = "/Login/Index";
        });
    } else {
        var loadUrl = "/tools/WebAjax.ashx?systemId=" + prodcutId + "&member=" + memberID + "&action=guanzhuclick";
        //判断当前用户是否已经关注过该项目
        $.post("/UserProject/getAttention", { id: prodcutId, mid: memberID }, function (data) {
            if (data == 0) {
                jQuery.ajax({
                    url: loadUrl,
                    scriptCharset: "utf-8",
                    success: function (data) {
                        var a = eval('(' + data + ')');
                        if (a == null) {
                            return;
                        }
                        var up = a.praise;
                        if (up > 10000) {
                            up = parseInt(up / 10000);
                            up = up + '万';
                        } else if (up > 1000) {
                            up = parseInt(up / 1000);
                            up = up + '千';
                        }
                        addCookie('Praise_System_Id_' + prodcutId, 'true', 0);
                        var t = (parseInt($("#guanzhu_" + prodcutId).position().left) + 10, parseInt($("#guanzhu_" + prodcutId).position().top) - 10, $("#guanzhu_" + prodcutId));
                        return $("#guanzhu_" + prodcutId).append('<div class="add"><b>+1</b></div>'), $(".add").css({
                            position: "absolute",
                            "z-index": "100",
                            left: "20px",
                            top: "-20px"
                        }),
                        $(".add").animate({ left: 0, top: 10, opacity: 0 }, 100, function () {
                            t.find("span").text(up);

                        }),
                        $("#guanzhu_" + prodcutId).addClass("cur");
                    }
                });
            }
            else {
                $.dialog.tips("已关注");
                $("#guanzhu_" + prodcutId).addClass("cur");
            }
        })
    }
}



function changeURLArg(url, arg, arg_val) {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + arg_val;
    if (url.match(pattern)) {
        var tmp = '/(' + arg + '=)([^&]*)/gi';
        tmp = url.replace(eval(tmp), replaceText);
        return tmp;
    } else {
        if (url.match('[\?]')) {
            return url + '&' + replaceText;
        } else {
            return url + '?' + replaceText;
        }
    }
    return url + '\n' + arg + '\n' + arg_val;
}

function GetUrlParms() {

    var args = new Object();

    var query = location.search.substring(1);//获取查询串   

    var pairs = query.split("&");//在逗号处断开   

    for (var i = 0; i < pairs.length; i++) {

        var pos = pairs[i].indexOf('=');//查找name=value   

        if (pos == -1) continue;//如果没有找到就跳过   

        var argname = pairs[i].substring(0, pos);//提取name   

        var value = pairs[i].substring(pos + 1);//提取value   

        args[argname] = unescape(value);//存为属性   

    }

    return args;

}


function goTop() {
    $(window).scroll(function (e) {
        //若滚动条离顶部大于100元素
        if ($(window).scrollTop() > 100)
            $("#gotop").fadeIn(1000);//以1秒的间隔渐显id=gotop的元素
        else
            $("#gotop").fadeOut(1000);//以1秒的间隔渐隐id=gotop的元素
    });
};